﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using Twitter.Tests;
using System.ComponentModel.Design;
using FriendsNearby.Services;
using FriendsNearby.Services.Tests.Mocks;
using Twitter;
using Moq;
using FriendsNearby.Services.Entities;


namespace FriendsNearbyBot.Tests
{
    [TestFixture]
    public class FriendsProcessorFixture : TestBase
    {
        [Test]
        public void ICanListMyFriends()
        {
            // Warn: 
            var services = new FacadeWithMocks();
            services.MockUsersService.Expect(x => x.GetFacebookUserIdFromTwitter("fnbtestb")).Returns("1234");
            services.MockPeopleNearbyService.Expect(x => x.GetPeopleNearby("1234")).Returns(
                new PersonNearby[]{ 
                    new PersonNearby { Relation = PersonNearbyRelation.Friend },
                    new PersonNearby { Relation = PersonNearbyRelation.Friend }
                });

            MemoryBotClient bot = new MemoryBotClient("fnb");
            MemoryBotClient user = new MemoryBotClient("fnbtestb");
            bot.SetBuddy(user);
            user.SetBuddy(bot);
            
            FriendsProcessor processor = new FriendsProcessor(services, CreateMessageBuilder());

            processor.Process(bot, CreateMessage("friends", user.ScreenName, bot.ScreenName));            

            //The message is splitted into 2 messages so the one I pick up is the last one.
            Message msg = user.Messages.GetAll().Last();
            string text = msg.Text;
            AssertString.StartsWith("You have 2 friends nearby (nearest", msg.Text);
        }

        private IServiceProvider CreateServices()
        {
            return (new FriendsNearbyFacade());
        }

        class FacadeWithMocks : FriendsNearbyFacade
        {
            public Mock<IUsersService> MockUsersService = new Mock<IUsersService>();
            public Mock<IPeopleNearbyService> MockPeopleNearbyService = new Mock<IPeopleNearbyService>();

            protected override IUsersService BuildUsersService()
            {
                return MockUsersService.Object;
            }

            protected override IPeopleNearbyService BuildPeopleNearbyService()
            {
                return MockPeopleNearbyService.Object;
            }
        }
    }
}

